<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="故障名称" prop="faultName">
        <el-input
          v-model="queryParams.faultName"
          placeholder="请输入故障名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维修人员" prop="technician">
        <el-input
          v-model="queryParams.technician"
          placeholder="请输入维修人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="故障开始时间" prop="startTime">
        <el-date-picker clearable
                        v-model="queryParams.startTime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm"
                        placeholder="请选择故障开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="停机时间" prop="stopTime">
        <el-input
          v-model="queryParams.stopTime"
          placeholder="请输入停机时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="故障原因一级分类" prop="faultCauseOne">
        <el-select v-model="queryParams.faultCauseOne" placeholder="请选择故障原因一级分类" clearable>
          <el-option
            v-for="dict in dict.type.gelly_fault_cause_one"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="故障原因二级分类" prop="faultCauseTwo">
        <el-select v-model="queryParams.faultCauseTwo" placeholder="请选择故障原因二级分类" clearable>
          <el-option
            v-for="dict in dict.type.gelly_fault_cause_two"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="维修时间" prop="repairTime">
        <el-input
          v-model="queryParams.repairTime"
          placeholder="请输入维修时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备件等待时间" prop="waitTime">
        <el-input
          v-model="queryParams.waitTime"
          placeholder="请输入备件等待时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="厂家服务时间" prop="serviceTime">
        <el-input
          v-model="queryParams.serviceTime"
          placeholder="请输入厂家服务时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="自主维修时间" prop="selfTime">
        <el-input
          v-model="queryParams.selfTime"
          placeholder="请输入自主维修时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="其他时间" prop="otherTime">
        <el-input
          v-model="queryParams.otherTime"
          placeholder="请输入其他时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['gelly:fault:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['gelly:fault:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['gelly:fault:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['gelly:fault:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="faultList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="故障ID" align="center" prop="faultId" />
      <el-table-column label="故障名称" align="center" prop="faultName" />
      <el-table-column label="失效模式" align="center" prop="failureType" />
      <el-table-column label="问题描述" align="center" prop="problemDesc" />
      <el-table-column label="根本原因" align="center" prop="rootCause" />
      <el-table-column label="具体维修措施" align="center" prop="repairAction" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="维修人员" align="center" prop="technician" />
      <el-table-column label="故障开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="停机时间" align="center" prop="stopTime" />
      <el-table-column label="故障原因一级分类" align="center" prop="faultCauseOne">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gelly_fault_cause_one" :value="scope.row.faultCauseOne"/>
        </template>
      </el-table-column>
      <el-table-column label="故障原因二级分类" align="center" prop="faultCauseTwo">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gelly_fault_cause_two" :value="scope.row.faultCauseTwo"/>
        </template>
      </el-table-column>
      <el-table-column label="维修时间" align="center" prop="repairTime" />
      <el-table-column label="备件等待时间" align="center" prop="waitTime" />
      <el-table-column label="厂家服务时间" align="center" prop="serviceTime" />
      <el-table-column label="自主维修时间" align="center" prop="selfTime" />
      <el-table-column label="其他时间" align="center" prop="otherTime" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="物料编码" align="center" prop="gellyPartList[0].itemCode" />
      <el-table-column label="备件名称" align="center" prop="gellyPartList[0].partName" />
      <el-table-column label="备件型号" align="center" prop="gellyPartList[0].partModel" />
      <el-table-column label="备件品牌" align="center" prop="gellyPartList[0].brand" />
      <el-table-column label="消耗数量" align="center" prop="gellyPartList[0].quantityUsed" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['gelly:fault:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gelly:fault:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改故障对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="故障名称" prop="faultName">
          <el-input v-model="form.faultName" placeholder="请输入故障名称" />
        </el-form-item>
        <el-form-item label="问题描述" prop="problemDesc">
          <el-input v-model="form.problemDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="根本原因" prop="rootCause">
          <el-input v-model="form.rootCause" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="具体维修措施" prop="repairAction">
          <el-input v-model="form.repairAction" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="维修人员" prop="technician">
          <el-input v-model="form.technician" placeholder="请输入维修人员" />
        </el-form-item>
        <el-form-item label="故障开始时间" prop="startTime">
          <el-date-picker clearable
                          v-model="form.startTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm"
                          placeholder="请选择故障开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="停机时间" prop="stopTime">
          <el-input v-model="form.stopTime" placeholder="请输入停机时间" />
        </el-form-item>
        <el-form-item label="故障原因一级分类" prop="faultCauseOne">
          <el-select v-model="form.faultCauseOne" placeholder="请选择故障原因一级分类">
            <el-option
              v-for="dict in dict.type.gelly_fault_cause_one"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="故障原因二级分类" prop="faultCauseTwo">
          <el-select v-model="form.faultCauseTwo" placeholder="请选择故障原因二级分类">
            <el-option
              v-for="dict in dict.type.gelly_fault_cause_two"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维修时间" prop="repairTime">
          <el-input v-model="form.repairTime" placeholder="请输入维修时间" />
        </el-form-item>
        <el-form-item label="备件等待时间" prop="waitTime">
          <el-input v-model="form.waitTime" placeholder="请输入备件等待时间" />
        </el-form-item>
        <el-form-item label="厂家服务时间" prop="serviceTime">
          <el-input v-model="form.serviceTime" placeholder="请输入厂家服务时间" />
        </el-form-item>
        <el-form-item label="自主维修时间" prop="selfTime">
          <el-input v-model="form.selfTime" placeholder="请输入自主维修时间" />
        </el-form-item>
        <el-form-item label="其他时间" prop="otherTime">
          <el-input v-model="form.otherTime" placeholder="请输入其他时间" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-divider content-position="center">备件信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddGellyPart">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteGellyPart">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="gellyPartList" :row-class-name="rowGellyPartIndex" @selection-change="handleGellyPartSelectionChange" ref="gellyPart">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="物料编码" prop="itemCode" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.itemCode" placeholder="请输入物料编码" />
            </template>
          </el-table-column>
          <el-table-column label="备件名称" prop="partName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.partName" placeholder="请输入备件名称" />
            </template>
          </el-table-column>
          <el-table-column label="备件型号" prop="partModel" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.partModel" placeholder="请输入备件型号" />
            </template>
          </el-table-column>
          <el-table-column label="备件品牌" prop="brand" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.brand" placeholder="请输入备件品牌" />
            </template>
          </el-table-column>
          <el-table-column label="消耗数量" prop="quantityUsed" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.quantityUsed" placeholder="请输入消耗数量" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFaultByDeviceId, getFault, delFaultByDeviceId, addFaultByDeviceId, updateFault } from "@/api/gelly/fault";

export default {
  name: "Fault",
  dicts: ['gelly_fault_cause_two', 'gelly_fault_cause_one'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedGellyPart: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 故障表格数据
      faultList: [],
      // 备件表格数据
      gellyPartList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        faultName: null,
        failureType: null,
        problemDesc: null,
        rootCause: null,
        repairAction: null,
        technician: null,
        startTime: null,
        stopTime: null,
        faultCauseOne: null,
        faultCauseTwo: null,
        repairTime: null,
        waitTime: null,
        serviceTime: null,
        selfTime: null,
        otherTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      deviceId: null
    };
  },
  created() {
    const id = this.$route.params && this.$route.params.deviceId;
    this.deviceId = id;
    this.getList();
  },
  methods: {
    /** 查询故障列表 */
    getList() {
      this.loading = true;
      listFaultByDeviceId(this.deviceId).then(response => {
        this.faultList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        faultId: null,
        faultName: null,
        failureType: null,
        problemDesc: null,
        rootCause: null,
        repairAction: null,
        technician: null,
        startTime: null,
        stopTime: null,
        faultCauseOne: null,
        faultCauseTwo: null,
        repairTime: null,
        waitTime: null,
        serviceTime: null,
        selfTime: null,
        otherTime: null,
        remark: null
      };
      this.gellyPartList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.faultId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加故障";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const faultId = row.faultId || this.ids
      getFault(faultId).then(response => {
        this.form = response.data;
        this.gellyPartList = response.data.gellyPartList;
        this.open = true;
        this.title = "修改故障";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.gellyPartList = this.gellyPartList;
          if (this.form.faultId != null) {
            updateFault(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const addData = {
              "deviceId": this.deviceId,
              "gellyFault":this.form
            }
            addFaultByDeviceId(addData).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const faultIds = row.faultId || this.ids;
      const deviceId1 = this.deviceId;
      this.$modal.confirm('是否确认删除故障编号为"' + faultIds + '"的数据项？').then(function() {
        return delFaultByDeviceId(deviceId1,faultIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 备件序号 */
    rowGellyPartIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 备件添加按钮操作 */
    handleAddGellyPart() {
      let obj = {};
      obj.itemCode = "";
      obj.partName = "";
      obj.partModel = "";
      obj.brand = "";
      obj.quantityUsed = "";
      this.gellyPartList.push(obj);
    },
    /** 备件删除按钮操作 */
    handleDeleteGellyPart() {
      if (this.checkedGellyPart.length == 0) {
        this.$modal.msgError("请先选择要删除的备件数据");
      } else {
        const gellyPartList = this.gellyPartList;
        const checkedGellyPart = this.checkedGellyPart;
        this.gellyPartList = gellyPartList.filter(function(item) {
          return checkedGellyPart.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleGellyPartSelectionChange(selection) {
      this.checkedGellyPart = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gelly/fault/export', {
        ...this.queryParams
      }, `fault_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="css">
.el-tooltip__popper{
  font-size: 14px;
  max-width:50%;
  backgroud: #68859a !important;  /*背景色　　!important优先级*/

} /*设置显示隐藏部分内容，按50%显示*/

</style>
